package ch6;

/**
 * @author ANG.YU
 * @date 2020-08-11 13:41
 * 构造函数的复合初始化
 * 请注意在Bath 构建器中，在所有初始化开始之前执行了一个语句。如果不在定义时进行初始化，仍然不能保
 * 证能在将一条消息发给一个对象句柄之前会执行任何初始化——除非出现不可避免的运行期违例。
 */
public class Bath {
    Soap castille;
    int i;
    float toy;
    private String
            s1 = new String("Happy"),
            s2 = "Happy",
            s3, s4;

    Bath() {
        System.out.println("Inside Bath()");
        s3 = new String("Joy");
        i = 47;
        toy = 3.14f;
        castille = new Soap();
    }

    public static void main(String[] args) {
        Bath bath = new Bath();
        bath.print();
    }

    void print() {
        if (s4 == null) {
            s4 = new String("Joy");
        }
        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);
        System.out.println("s3 = " + s3);
        System.out.println("s4 = " + s4);
        System.out.println("i = " + i);
        System.out.println("toy = " + toy);
        System.out.println("castille = " + castille);

    }
}

class Soap {
    private String s;

    Soap() {
        System.out.println("Soap()");
        s = new String("Constructed");
    }

    @Override
    public String toString() {
        return s;
    }
}
